package com.dfxy.blog.window;

import com.dfxy.blog.dao.UserDao;
import com.dfxy.blog.listener.LogInActionListener;
import com.dfxy.blog.listener.LogOnActionListener;
import com.dfxy.blog.util.DBUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.SQLException;

public class LoginWindow extends JFrame {

    JLabel labelUserName = new JLabel("用户名");
    JLabel labelPassword = new JLabel("密  码");

    JTextField textFieldUserName = new JTextField();
    JTextField textFieldPassword = new JTextField();

    JButton buttonLogIn = new JButton("登录");
    JButton buttonLogOn = new JButton("注册");

    private Connection con;

    public LoginWindow() throws SQLException, ClassNotFoundException {
        this.setTitle("登录窗口");
        this.setSize(400, 300);

        Container cp = this.getContentPane();
        cp.setLayout(null);

        con = DBUtil.getConnection();
        UserDao userDao = new UserDao();

        labelUserName.setBounds(60,50,100,20);
        labelPassword.setBounds(60,100,100,20);
        textFieldUserName.setBounds(170, 50, 100, 20);
        textFieldPassword.setBounds(170, 100, 100, 20);

        cp.add(labelUserName);
        cp.add(labelPassword);
        cp.add(textFieldUserName);
        cp.add(textFieldPassword);

        buttonLogIn.setBounds(60, 150, 80, 20);
        buttonLogOn.setBounds(170, 150, 80, 20);

        buttonLogIn.addActionListener(
                new LogInActionListener(
                        textFieldUserName,
                        textFieldPassword,
                        con,
                        userDao,
                        cp,
                        this
                )
        );

        buttonLogOn.addActionListener(
                new LogOnActionListener(
                        textFieldUserName,
                        textFieldPassword,
                        con,
                        userDao,
                        cp
                )
        );

        cp.add(buttonLogIn);
        cp.add(buttonLogOn);
    }

    public static void main(String args[]) throws SQLException, ClassNotFoundException {
        LoginWindow loginWindow = new LoginWindow();
        loginWindow.setVisible(true);
    }
}
